package com.zyf.chapter01.principle.SOLID.OCP;

import com.zyf.chapter01.principle.SOLID.OCP.handler.ErrorAlertHandler;
import com.zyf.chapter01.principle.SOLID.OCP.handler.TimeoutAlertHandler;
import com.zyf.chapter01.principle.SOLID.OCP.handler.TpsAlertHandler;

/**
 * 上下文注册
 * @author yifeng
 */
public class ApplicationContext {
    private AlertRule alertRule;
    private Notification notification;
    private Alert.RefactorFirstAlert alert;

    public void initialzeBeans() {
        // 省略一些初始化代码
        alertRule = new AlertRule(0, 0, 0);
        notification = new Notification(/* 省略参数 */);
        this.alert = new Alert().new RefactorFirstAlert();
        this.alert.addAlertHandler(new TpsAlertHandler(alertRule, notification));
        this.alert.addAlertHandler(new ErrorAlertHandler(alertRule, notification));
        // 改动三：注册handler
        this.alert.addAlertHandler(new TimeoutAlertHandler(alertRule, notification));
    }

    public Alert.RefactorFirstAlert getAlert() {
        return alert;
    }

    public static final ApplicationContext instance = new ApplicationContext();
    private ApplicationContext() {
        instance.initialzeBeans();
    }
    public static ApplicationContext getInstance(){
        return instance;
    }
}


